package com.kgc.community.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.Stall;
import com.kgc.community.pojo.StallExample;
import com.kgc.community.service.StallService;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class StallController {
    @Resource
    StallService stallService;
    //增加方法
    @RequestMapping("add.do")
    public String add(){
        Integer car_state = 0;
        Stall stall = new Stall();
        stall.setCarState(car_state);
        int res = stallService.addStall(stall);
        //System.out.println(res);
        return "redirect:stall.do";
    }
    //根据id查询方法
    @RequestMapping("selectById.do")
    public String selectById(Integer id,Model model){
        Stall stall = stallService.selectById(id);
        model.addAttribute("stall",stall);
        return "park_add";
    }

    //修改方法
    @RequestMapping("update.do")
    public String updateById(String carid, String color, String times, Integer id, Integer status, Model model, PrintWriter out) throws ParseException {
        System.out.println("*****************"+color);
        Date time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        time = sdf.parse(times);
        Stall stall = new Stall(id,carid,color,status,time);
        System.out.println("*****************"+times);
        if(stallService.updateById(stall)>0){
            System.out.println("*****************************123");
            out.print("<script>alert('修改成功');location.href='/stall.do'</script>");
        }/*else {
            System.out.println("***********************000");
            out.print("<script>alert('修改有错误')</script>");
            return "redirect:selectById.do";
        }*/
        return "redirect:stall.do";
    }

    //查询遍历的方法
    @RequestMapping("stall.do")
    public String selectAll(String username, Integer pageNum, Model model){

        Integer pageSize = 3;
        if(pageNum == null){
            pageNum=1;
        }
        StallExample example = new StallExample();

       if(username!=null&&username!=""){
           example.createCriteria().andCarIdLike("%"+username+"%");
       }

      /*  Stall stall = new Stall();
        stall.setParkingdeadline(time);
        stall.setParkingdeadline(time1);
        stall.setCarId(username);*/
        PageInfo<Stall> pageInfo = stallService.selectAll(example, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "parkinglist";
    }
}
